package leftMain;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

public class RunTime extends Thread
{
	public int n;
	public boolean cont = false;
	
	public char cLang;
	
	public Vector<String> pos_1;
	public Vector<String> pos_2;
	
	public JFrame QFrame = new JFrame("Add word?");
		
	public JLabel label_word = new JLabel("Word:*");
	public JLabel label_pos = new JLabel("Part of Speech:*");
	public JTextField word = new JTextField("sample");
	//public JTextField pos = new JTextField();
	public JComboBox pos = new JComboBox();
	
	public JSeparator separator_1 = new JSeparator();
	public JSeparator separator_2 = new JSeparator();
	public JLabel label_1 = new JLabel("Translation");
		
	public JLabel label_translation_word = new JLabel("Word:*");
	public JLabel label_translation_pos = new JLabel("Part of Speech:*");
	public JTextField translation_word = new JTextField();
	//public JTextField translation_pos = new JTextField();
	public JComboBox translation_pos = new JComboBox();
	
	public JButton add = new JButton("Add");
	public JButton cancel = new JButton("Cancel");
	
	public Scanner s;
	public LeftGUI gui;
	
	public RunTime(char lang, Scanner s, LeftGUI gui)
	{
		n = 0;
		this.s = s;
		this.gui = gui;
		cLang = lang;
		run();
	}
	
	public void run()
	{	
		//gui.repaint();
		while(n <  10)
		{
		
		QFrame = new JFrame("Add word? _ " + n);
		
		label_word = new JLabel("Word:*");
		label_pos = new JLabel("Part of Speech:*");
	  	word = new JTextField("sample");
	  	pos = new JComboBox();

	  	separator_1 = new JSeparator();
	  	separator_2 = new JSeparator();
	  	label_1 = new JLabel("Translation");

	  	label_translation_word = new JLabel("Word:*");
	  	label_translation_pos = new JLabel("Part of Speech:*");
	  	translation_word = new JTextField();
	  	translation_pos = new JComboBox();
	
	  	add = new JButton("Add");
	  	cancel = new JButton("Cancel");

		textReader('1');
		textReader('2');
		int a = 0;
		pos.addItem("Please Choose . . .");
		translation_pos.addItem("Please Choose . . .");
		if(cLang == '1')
		{
			while(a < pos_1.size())
			{
				pos.addItem(pos_1.get(a));
				a++;
			}
			a= 0;
			while(a < pos_1.size())
			{
				translation_pos.addItem(pos_2.get(a));
				a++;
			}
		}
		else
		{
			while(a < pos_2.size())
			{
				pos.addItem(pos_2.get(a));
				a++;
			}
			a = 0;
			while(a < pos_1.size())
			{
				translation_pos.addItem(pos_1.get(a));
				a++;
			}
		}
		
		QFrame.setBounds(0, 0, 420, 215);
		QFrame.getContentPane().setLayout(null);
		QFrame.setResizable(false);
		QFrame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				QFrame.dispose();
				//System.exit(0);
			}
		});
		
		//WORD:
		label_word.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		label_word.setBounds(15, 7, 160, 25);
		QFrame.getContentPane().add(label_word);
		
		word.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		word.setText("");
		word.setEditable(true);
		word.setBackground(Color.white);
		word.setBounds(165, 7, 230, 25);
		word.setEditable(false);
		QFrame.getContentPane().add(word);
		
		//PART OF SPEECH
		label_pos.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		label_pos.setBounds(15, 37, 160, 25);
		QFrame.getContentPane().add(label_pos);
		
		pos.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		pos.setBackground(Color.white);
		pos.setBounds(165, 37, 230, 25);
		QFrame.getContentPane().add(pos);
		
		//SEPARATOR
		separator_1.setBounds(15, 75, 150, 5);
		QFrame.getContentPane().add(separator_1);
		
		separator_2.setBounds(245, 75, 150, 5);
		QFrame.getContentPane().add(separator_2);
		
		//LABEL_1
		label_1.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 12));
		label_1.setBounds(168, 70, 90, 10);
		QFrame.getContentPane().add(label_1);
		
		//TRANSLATION_WORD
		label_translation_word.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		label_translation_word.setBounds(15, 90, 160, 25);
		QFrame.getContentPane().add(label_translation_word);
		
		translation_word.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		translation_word.setText("");
		translation_word.setEditable(true);
		translation_word.setBackground(Color.white);
		translation_word.setBounds(165, 90, 230, 25);
		QFrame.getContentPane().add(translation_word);
		
		
		//TRANSLATE_POS
		label_translation_pos.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		label_translation_pos.setBounds(15, 120, 160, 25);
		QFrame.getContentPane().add(label_translation_pos);
		
		translation_pos.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		translation_pos.setBackground(Color.white);
		translation_pos.setBounds(165, 120, 230, 25);
		QFrame.getContentPane().add(translation_pos);
		
		//BUTTONS
		add.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		add.setToolTipText("Add");
		add.setBounds(195, 150, 100, 25);
		add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if(pos.getSelectedIndex() != 0 && !translation_word.getText().equals("") && translation_pos.getSelectedIndex() != 0)
            	{
            		javax.swing.JOptionPane.showMessageDialog(QFrame, "Word successfully added.");
            		QFrame.dispose();
            		cont = true;
            		
            		//resume();
            	}
            	else
            		javax.swing.JOptionPane.showMessageDialog(QFrame, "Required fields missing. (*)", "Error!!", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        });
		QFrame.getContentPane().add(add);
		
		cancel.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		cancel.setToolTipText("Cancel");
		cancel.setBounds(295, 150, 100, 25);
		cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
					int answer = javax.swing.JOptionPane.showConfirmDialog(QFrame, "You have not yet added the word.\nWould you like to add it first?", "Warning!!", javax.swing.JOptionPane.YES_NO_OPTION);
                	if(answer == 1)
                	{
                		javax.swing.JOptionPane.showMessageDialog(QFrame, "Warning!! Sentence will not be translated.", "Warning!!", javax.swing.JOptionPane.ERROR_MESSAGE);
                		QFrame.dispose();
                		cont = true;
                		
                		
                		//resume();
                	}
                	answer = -1;
                	
            }
        });
		QFrame.getContentPane().add(cancel);
		
		QFrame.setVisible(true);
		gui.repaint();
		QFrame.repaint();
		
		
		
		while(!cont)
		{
			gui.repaint();
			QFrame.repaint();
		}
		n++;
		cont = false;
		gui.repaint();
		}
		
	}
	
	public void textReader(char lang)
	{
		String textmessage = "";	
		StringTokenizer tokens;
		String filename;
		
		if(lang == '1')
		{
			pos_1 = new Vector();
			filename = "/home/pckid/LEFT/language1.txt";
		}
		else
		{
			pos_2 = new Vector();
			filename = "/home/pckid/LEFT/language2.txt";
		}
		
		try
		{
			BufferedReader in = new BufferedReader (new FileReader (filename));
			String line;
			
			while ((line = in.readLine()) != null)
			{					
				textmessage = textmessage + line;
				textmessage = textmessage + "\n";
			}
			tokens = new StringTokenizer(textmessage);
			if(lang == '1')
				pos_1 = get_pos(tokens);
			else
				pos_2 = get_pos(tokens);
			
			in.close();
		}
		catch (IOException ex1)
		{
		  	
      		System.err.println("IO Exception (main): " + ex1.getMessage());
       	}	
	}
	
	public Vector<String> get_pos(StringTokenizer strTokens)
	{
		boolean b = true;
		Vector<String> pos = new Vector();
		while(strTokens.hasMoreElements() && b)
		{
			if(strTokens.nextElement().equals("Terminals:"))
			{
				String temp = "";
				while(b)
				{
					temp = strTokens.nextElement().toString();
					if(!temp.equals("NonTerminals:"))
						pos.add(temp);
					else
						b = false;
				}
			}
		}
		return pos;
	}
	
}